package com.tanhua.manage.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 统一异常处理
 */
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleExcepting(Exception e) {
        log.error("系统发送异常：", e);

        String message = "服务器内部错误";

        // 如果是自定义的异常
        if(e instanceof BusinessException) {
            message = ((BusinessException)e).getMessage();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new CommonResponse(message));
    }
}